package com.xiegang.servicedriveruser.controller;

import com.xiegang.internalcommon.constant.DriverCarContants;
import com.xiegang.internalcommon.dto.DriverUser;
import com.xiegang.internalcommon.dto.ResponseResult;
import com.xiegang.internalcommon.response.DriverUserExistsResponse;
import com.xiegang.internalcommon.response.OrderDriverResponse;
import com.xiegang.servicedriveruser.service.DriverUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @author xieGang
 * @description
 * @date 2023/3/29 13:34
 **/
@RestController
public class UserController {

    @Autowired
    private DriverUserService driverUserService;


    @PostMapping("/user")
    public ResponseResult addUser(@RequestBody DriverUser driverUser) {
        return driverUserService.addUser(driverUser);
    }

    @PutMapping("/user")
    public ResponseResult updateUser(@RequestBody DriverUser driverUser) {

        LocalDateTime now = LocalDateTime.now();
        driverUser.setGmtModified(now);

        return driverUserService.updateUser(driverUser);
    }

    @GetMapping("/check-driver/{driverPhone}")
    public ResponseResult getUser(@PathVariable("driverPhone") String driverPhone) {
        ResponseResult<DriverUser> user = driverUserService.getUser(driverPhone);
        String driverPhoneDB = user.getData() == null ? null : user.getData().getDriverPhone();
        int ifExists = StringUtils.isBlank(driverPhoneDB) ? DriverCarContants.DRIVER_NOT_EXISTS : DriverCarContants.DRIVER_EXISTS;

        DriverUserExistsResponse driverUserExistsResponse = new DriverUserExistsResponse();
        driverUserExistsResponse.setDriverPhone(driverPhoneDB);
        driverUserExistsResponse.setIfExists(ifExists);
        return ResponseResult.success(driverUserExistsResponse);
    }

    /**
     * 根据车辆id查询订单需要的司机信息
     * @param carId
     * @return
     */
    @GetMapping("/get-available-driver/{carId}")
    public ResponseResult<OrderDriverResponse> getAvailableDriver(@PathVariable("carId")Long carId){
        return driverUserService.getAvailableDriver(carId);
    }


}
